package com.zues.thread.create;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * FutureTask使用案例
 * 模拟同时查询用户数据和部门数据
 * 必须两种数据都查询出来之后才能继续接着的操作
 *
 * @author zues
 * @date 12/7/2024 10:31 下午
 */
@Slf4j(topic = "c.FutureTaskDemo")
public class FutureTaskDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> userTask = new FutureTask<String>(() -> {
            log.info("模拟耗时操作");
            Thread.sleep(3000);
            return "用户数据";
        });

        FutureTask<String> deptTask = new FutureTask<String>(() -> {
            return "部门数据";
        });

        new Thread(userTask,"userTask").start();
        new Thread(deptTask,"deptTask").start();

        log.info("用户数据 = {}",userTask.get());
        log.info("部门数据 = {}",deptTask.get());
        log.info("全部数据查询完成");

    }

}
